﻿using System.Configuration;

namespace EasyLib.Configuration
{
    public sealed class DatabaseConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("databaseConfig")]
        public DatabaseConfigElementCollection DatabaseConfig
        {
            get
            {
                return this["databaseConfig"] as DatabaseConfigElementCollection;
            }
            set
            {
                this["databaseConfig"] = value;
            }
        }
    }

    public sealed class DatabaseConfigElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get
            {
                return this["name"] as string;
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("connectionStringName")]
        public string ConnectionStringName
        {
            get
            {
                return this["connectionStringName"] as string;
            }
            set
            {
                this["connectionStringName"] = value;
            }
        }

        //[ConfigurationProperty("connectionString")]
        //public string ConnectionString
        //{
        //    get
        //    {
        //        return this["connectionString"] as string;
        //    }
        //    set
        //    {
        //        this["connectionString"] = value;
        //    }
        //}
    }

    public sealed class DatabaseConfigElementCollection : ConfigurationElementCollection
    {
        # region Overrided Methods&Properties

        protected override ConfigurationElement CreateNewElement()
        {
            return new DatabaseConfigElement();
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            DatabaseConfigElement elem = element as DatabaseConfigElement;
            return elem.ConnectionStringName + elem.Name;
        }

        //Gets a value indicating whether an attempt to add a duplicate ConfigurationElement to the ConfigurationElementCollection 
        //will cause an exception to be thrown.
        protected override bool ThrowOnDuplicate
        {
            //Note that elements with identical keys and values are not considered duplicates, and are accepted silently. 
            //Only elements with identical keys but different values are considered duplicates.                       
            get
            {
                return true;
            }
        }

        protected override string ElementName
        {
            get
            {
                return "table";
            }
        }

        #endregion

        #region Customize Indexer

        public DatabaseConfigElement this[int index]
        {
            get
            {
                return BaseGet(index) as DatabaseConfigElement;
            }
        }

        #endregion
    }
}